package managedbean.Sales;

import javax.faces.event.ActionEvent;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.ejb.EJB;
import session.stateless.EmployeeSessionBeanLocal;
import session.stateless.LeadSessionBeanLocal;
import javax.faces.bean.ManagedProperty;
import managedbean.UserSessionManagerBean;
import entity.Address;
import entity.Employee;
import entity.Lead;
import java.util.ArrayList;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import session.stateless.LogSessionBeanLocal;

@ManagedBean(name = "leadManagerBean")
@RequestScoped
public class LeadManagerBean {

    @ManagedProperty(value = "#{userSessionManagerBean}")
    private UserSessionManagerBean userSessionManagerBean;
    @EJB
    private EmployeeSessionBeanLocal employeeSessionBeanLocal;
    @EJB
    private LeadSessionBeanLocal leadSessionBeanLocal;
    @EJB
    private LogSessionBeanLocal logSessionBeanLocal;
    private String loadAddLead;
    private String loadUpdateLead;
    private String loadSearchLead;
    private Long custId;
    private String userId;
    private Employee employee;
    private String email;
    private String firstName;
    private String lastName;
    private String company;
    private String title;
    private String status;
    private String industry;
    private Integer phone;
    private Address companyAddress;
    private String street;
    private String country;
    private String city;
    private Integer postal;
    private Double revenue;
    private Integer numEmployee;
    private String description;
    private static Lead lead;
    private ArrayList<Lead> leads;
    private String request;

    public String getLoadAddLead() {
        userId = userSessionManagerBean.getUserId();
        if (userSessionManagerBean.getRequest() != null) {
            if (userSessionManagerBean.getRequest().compareTo("addSalesContact.xhtml") == 0) {
                lead = (Lead) userSessionManagerBean.getSelectedItem();
                email = lead.getEmail();
            }
        }
        return null;
    }

    public String getLoadUpdateLead() {

        try {

            lead = (Lead) userSessionManagerBean.getSelectedItem();
            userSessionManagerBean.setSelectedLead(lead);
            custId = lead.getCustId();
            email = lead.getEmail();
            userId = lead.getEmployee().getUserId();
            firstName = lead.getFirstName();
            lastName = lead.getLastName();
            company = lead.getCompany();
            title = lead.getTitle();
            status = lead.getStatus();
            industry = lead.getIndustry();
            phone = lead.getPhone();
            companyAddress = lead.getCompanyAddress();
            street = companyAddress.getStreet();
            country = companyAddress.getCountry();
            city = companyAddress.getCity();
            postal = companyAddress.getPostal();
            revenue = lead.getRevenue();
            numEmployee = lead.getNumEmployee();
            description = lead.getDescription();

        } catch (Exception exception) {
            try {

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort()
                        + "/MerlionERP-war/Web_Sales/searchLead.xhtml?request=updateLead");
            } catch (Exception ex) {
            }
        }

        return null;
    }

    public String getLoadSearchLead() {
        leads = leadSessionBeanLocal.getLeads();
        
        userId = userSessionManagerBean.getUserId();
        String temp = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("request");

        if (temp != null) {

            userSessionManagerBean.setSelectedItem((String) temp + ".xhtml");

            try {

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort() + "/MerlionERP-war/Web_Sales/searchLead.xhtml");

            } catch (Exception ex) {
            }
        }

         request = (String) userSessionManagerBean.getSelectedItem();
        
        
        return null;
    }

    // <editor-fold defaultstate="collapsed" desc="Getter & Setter methods. Click on the + sign on the left to edit the code.">
    /**
     * @return the employeeSessionLocal
     */
    public EmployeeSessionBeanLocal getEmployeeSessionBeanLocal() {
        return employeeSessionBeanLocal;
    }

    /**
     * @param employeeSessionLocal the employeeSessionLocal to set
     */
    public void setEmployeeSessionBeanLocal(EmployeeSessionBeanLocal employeeSessionBeanLocal) {
        this.employeeSessionBeanLocal = employeeSessionBeanLocal;
    }

    /**
     * @return the leadSessionLocal
     */
    public LeadSessionBeanLocal getLeadSessionBeanLocal() {
        return leadSessionBeanLocal;
    }

    /**
     * @param leadSessionLocal the leadSessionLocal to set
     */
    public void setLeadSessionLocal(LeadSessionBeanLocal leadSessionBeanLocal) {
        this.leadSessionBeanLocal = leadSessionBeanLocal;
    }

    /**
     * @return the userSessionManagerBean
     */
    public UserSessionManagerBean getUserSessionManagerBean() {
        return userSessionManagerBean;
    }

    /**
     * @param userSessionManagerBean the userSessionManagerBean to set
     */
    public void setUserSessionManagerBean(UserSessionManagerBean userSessionManagerBean) {
        this.userSessionManagerBean = userSessionManagerBean;
    }

    /**
     * @return the custId
     */
    public Long getCustId() {
        return custId;
    }

    /**
     * @param custId the custId to set
     */
    public void setCustId(Long custId) {
        this.custId = custId;
    }

    /**
     * @return the employee
     */
    public Employee getEmployee() {
        return employee;
    }

    /**
     * @param employee the employee to set
     */
    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return the company
     */
    public String getCompany() {
        return company;
    }

    /**
     * @param company the company to set
     */
    public void setCompany(String company) {
        this.company = company;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * @return the phone
     */
    public Integer getPhone() {
        return phone;
    }

    /**
     * @param phone the phone to set
     */
    public void setPhone(Integer phone) {
        this.phone = phone;
    }

    /**
     * @return the companyAddress
     */
    public Address getCompanyAddress() {
        return companyAddress;
    }

    /**
     * @param companyAddress the companyAddress to set
     */
    public void setCompanyAddress(Address companyAddress) {
        this.companyAddress = companyAddress;
    }

    /**
     * @return the revenue
     */
    public Double getRevenue() {
        return revenue;
    }

    /**
     * @param revenue the revenue to set
     */
    public void setRevenue(Double revenue) {
        this.revenue = revenue;
    }

    /**
     * @return the numEmployee
     */
    public Integer getNumEmployee() {
        return numEmployee;
    }

    /**
     * @param numEmployee the numEmployee to set
     */
    public void setNumEmployee(Integer numEmployee) {
        this.numEmployee = numEmployee;
    }

    /**
     * @return the industry
     */
    public String getIndustry() {
        return industry;
    }

    /**
     * @param industry the industry to set
     */
    public void setIndustry(String industry) {
        this.industry = industry;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the userId
     */
    public String getUserId() {
        return userId;
    }

    /**
     * @param userId the userId to set
     */
    public void setUserId(String userId) {
        this.userId = userId;
    }

    /**
     * @return the street
     */
    public String getStreet() {
        return street;
    }

    /**
     * @param street the street to set
     */
    public void setStreet(String street) {
        this.street = street;
    }

    /**
     * @return the country
     */
    public String getCountry() {
        return country;
    }

    /**
     * @param country the country to set
     */
    public void setCountry(String country) {
        this.country = country;
    }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    /**
     * @param city the city to set
     */
    public void setCity(String city) {
        this.city = city;
    }

    /**
     * @return the postal
     */
    public Integer getPostal() {
        return postal;
    }

    /**
     * @param postal the postal to set
     */
    public void setPostal(Integer postal) {
        this.postal = postal;
    }

    public void setLoadAddLead(String loadAddLead) {
        this.loadAddLead = loadAddLead;
    }

    public void setLoadUpdateLead(String loadUpdateLead) {
        this.loadUpdateLead = loadUpdateLead;
    }

    /**
     * @param loadSearchLead the loadSearchLead to set
     */
    public void setLoadSearchLead(String loadSearchLead) {
        this.loadSearchLead = loadSearchLead;
    }

    /**
     * @return the lead
     */
    public Lead getLead() {
        return lead;
    }

    /**
     * @param lead the lead to set
     */
    public void setLead(Lead lead) {
        this.lead = lead;
    }

    /**
     * @return the leads
     */
    public ArrayList<Lead> getLeads() {
        return leads;
    }

    /**
     * @param leads the leads to set
     */
    public void setLeads(ArrayList<Lead> leads) {
        this.leads = leads;
    }

    //</editor-fold>
    public void addLead(ActionEvent event) {
        companyAddress = new Address(street, country, city, postal, "Company");
        Lead tempLead = leadSessionBeanLocal.getLead(email);


        if (tempLead == null) //email not taken
        {

            leadSessionBeanLocal.addLead(userId, email, firstName, lastName, company, title, status, phone, companyAddress, revenue, description, numEmployee, industry);
            lead = leadSessionBeanLocal.getLead(email);

            logSessionBeanLocal.addLog("Add sales lead", "Sales lead account created: " + lead.getCustId().toString(), "addLead.xhtml", userSessionManagerBean.getUserId());

            lead = leadSessionBeanLocal.getLead(email);
            userSessionManagerBean.setSelectedLead(lead);


            try {
//access from addSalesContact
                if (userSessionManagerBean.getRequest() != null) {
                    FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                            + userSessionManagerBean.getServerName() + ":"
                            + userSessionManagerBean.getServerPort()
                            + "/MerlionERP-war/Web_Sales/inquiries.xhtml");
                } else {
                    ArrayList<String> al = new ArrayList<String>();
                    al.add("Sales lead created");
                    al.add("You have successfully created a sales lead.");
                    al.add("Click on the link to return to ");
                    al.add("continue");
                    al.add("Web_Sales/addLead.xhtml");
                    userSessionManagerBean.setSelectedItem(al);

                    FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                            + userSessionManagerBean.getServerName() + ":"
                            + userSessionManagerBean.getServerPort()
                            + "/MerlionERP-war/success.xhtml");
                }
            } catch (Exception ex) {
            }
        }






    }

    public void doneUpdate(ActionEvent event) {
        try {
            companyAddress = new Address(street, country, city, postal, "Company");
            Lead temp = leadSessionBeanLocal.getLead(email);
            if (temp == null) {
                getLeadSessionBeanLocal().updateLead(lead.getCustId(), lead.getEmployee().getUserId(), email, firstName, lastName, company, title, status, phone, companyAddress, revenue, description, numEmployee, industry);
            } else {
                if (temp.getCustId().compareTo(lead.getCustId()) == 0) {
                    getLeadSessionBeanLocal().updateLead(lead.getCustId(), lead.getEmployee().getUserId(), email, firstName, lastName, company, title, status, phone, companyAddress, revenue, description, numEmployee, industry);
                }
            }

            logSessionBeanLocal.addLog("Update sales lead", "Sales lead account updated: " + lead.getCustId().toString(), "updateLead.xhtml", userSessionManagerBean.getUserId());

            ArrayList<String> al = new ArrayList<String>();
            al.add("Sales lead updated");
            al.add("You have successfully updated the sales lead. ");
            al.add("Click on the link to return to ");
            al.add("continue");
            al.add("Web_Sales/searchLead.xhtml?request=updateLead");

            userSessionManagerBean.setSelectedItem(al);
            FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort() + "/MerlionERP-war/success.xhtml");

        } catch (Exception ex) {
        }

    }

    public void edit(ActionEvent event) {
        userSessionManagerBean.setSelectedItem((Lead) lead);
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort() + "/MerlionERP-war/Web_Sales/" + request);
        } catch (Exception ex) {
        }
    }
    
     public void back(ActionEvent e) {
        try {
            if (e.getComponent().getId().equals("btnBackToSearchLead")) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Sales/searchLead.xhtml?request=viewLead"); //go back to quotation.xhtml
            } 
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }
     
}